package spade;

import java.util.Date;
import java.util.Vector;

public class Envelope
{
	Vector<AID>	   to;
	AID	           from;
	String	       comments;
	String	       aclRepresentation;
	Integer	       payloadLength;
	String	       payloadEncoding;
	Date	       date;
	Vector<String>	encrypted;
	Vector<AID>	   intendedReceiver;
	// Vector<Object> received;
	Vector<Object>	transportBehaviour;

	public Envelope()
	{
		init(null, null, null, null, null, null, null, null, null, null);
	}
	public Envelope(Vector<AID> to, AID from, String comments,
	        String aclRepresentation, Integer payloadLength,
	        String payloadEncoding, Date date, Vector<String> encrypted,
	        Vector<AID> intendedReceiver, Vector<Object> transportBehaviour)
	{
		init(to, from, comments, aclRepresentation, payloadLength,
		        payloadEncoding, date, encrypted, intendedReceiver,
		        transportBehaviour);
	}
	void init(Vector<AID> to, AID from, String comments,
	        String aclRepresentation, Integer payloadLength,
	        String payloadEncoding, Date date, Vector<String> encrypted,
	        Vector<AID> intendedReceiver, Vector<Object> transportBehaviour)
	{
		this.to = to;
		this.from = from;
		this.comments = comments;
		this.aclRepresentation = aclRepresentation;
		this.payloadLength = payloadLength;
		this.payloadEncoding = payloadEncoding;
		this.date = date;
		this.encrypted = encrypted;
		this.intendedReceiver = intendedReceiver;
		// this.received = received;
		this.transportBehaviour = transportBehaviour;
	}
	public AID getFrom()
	{
		return from;
	}
	public void setFrom(AID from)
	{
		this.from = from;
	}
	public Vector<AID> getTo()
	{
		return to;
	}
	public void setTo(Vector<AID> to)
	{
		this.to = to;
	}
	public void addTo(AID a)
	{
		this.to.add(a);
	}
	public String getAclRepresentation()
	{
		return aclRepresentation;
	}
	public void setAclRepresentation(String aclRepresentation)
	{
		this.aclRepresentation = aclRepresentation;
	}
	public String getComments()
	{
		return comments;
	}
	public void setComments(String comments)
	{
		this.comments = comments;
	}
	public Date getDate()
	{
		return date;
	}
	public void setDate(Date date)
	{
		this.date = date;
	}
	public String getPayloadEncoding()
	{
		return payloadEncoding;
	}
	public void setPayloadEncoding(String payloadEncoding)
	{
		this.payloadEncoding = payloadEncoding;
	}
	public Integer getPayloadLength()
	{
		return payloadLength;
	}
	public void setPayloadLength(Integer payloadLength)
	{
		this.payloadLength = payloadLength;
	}
	public Vector<String> getEncrypted()
	{
		return encrypted;
	}
	public void setEncrypted(Vector<String> encrypted)
	{
		this.encrypted = encrypted;
	}
	public Vector<AID> getIntendedReceiver()
	{
		return intendedReceiver;
	}
	public void setIntendedReceiver(Vector<AID> intendedReceiver)
	{
		this.intendedReceiver = intendedReceiver;
	}
	public void addIntendedReceiver(AID recv)
	{
		this.intendedReceiver.add(recv);
	}
	// public Vector<Object> getReceived()
	// {
	// return received;
	// }
	// public void setReceived(Vector<Object> received)
	// {
	// this.received = received;
	// }
	public Vector<Object> getTransportBehaviour()
	{
		return transportBehaviour;
	}
	public void setTransportBehaviour(Vector<Object> transportBehaviour)
	{
		this.transportBehaviour = transportBehaviour;
	}

	public String toXML()
	{
		String r = "<?xml version=\"1.0\"?>" + "\n";
		r += "\t\t<envelope> \n";
		r += "\t\t\t<params index=\"1\">" + "\n";
		r += "\t\t\t\t<to>\n";
		for (AID aid : to)
		{
			r += "\t\t\t\t\t<agent-identifier> \n";
			r += "\t\t\t\t\t\t<name>" + aid.getName() + "</name> \n";
			r += "\t\t\t\t\t\t<addresses>\n";
			for (String addr : aid.getAddresses())
			{
				r += "\t\t\t\t\t\t\t<url>" + addr + "</url>\n";
			}
			r += "\t\t\t\t\t\t</addresses> \n";
			r += "\t\t\t\t\t</agent-identifier>\n";
		}
		r += "\t\t\t\t</to> \n";
		if (this.from != null)
		{
			r += "\t\t\t\t<from> \n";
			r += "\t\t\t\t\t<agent-identifier> \n";
			r += "\t\t\t\t\t\t<name>" + this.from.getName() + "</name> \n";
			r += "\t\t\t\t\t\t<addresses>\n";
			for (String addr : this.from.getAddresses())
			{
				r += "\t\t\t\t\t\t\t<url>" + addr + "</url>\n";
			}
			r += "\t\t\t\t\t\t</addresses> \n";
			r += "\t\t\t\t\t</agent-identifier> \n";
			r += "\t\t\t\t</from>\n";
		}
		if (this.aclRepresentation != null)
		{
			r +=
			        "\t\t\t\t<acl-representation>" + this.aclRepresentation
			                + "</acl-representation>\n";
		}
		if (this.payloadLength != null)
		{
			r +=
			        "\t\t\t\t<payload-length>" + this.payloadLength
			                + "</payload-length>\n";
		}
		if (this.payloadEncoding != null)
		{
			r +=
			        "\t\t\t\t<payload-encoding>" + this.payloadEncoding
			                + "</payload-encoding>\n";
		}
		if (this.date != null)
		{
			r += "\t\t\t\t<date>" + this.date.toString() + "</date>\n";
		}
		if (this.intendedReceiver != null)
		{
			r += "\t\t\t\t<intended-receiver>\n";
			for (AID aid : this.intendedReceiver)
			{
				r += "\t\t\t\t\t<agent-identifier> \n";
				r += "\t\t\t\t\t\t<name>" + aid.getName() + "</name> \n";
				r += "\t\t\t\t\t\t<addresses>\n";
				for (String addr : aid.getAddresses())
				{

					r += "\t\t\t\t\t\t\t<url>" + addr + "</url>\n";
				}
				r += "\t\t\t\t\t\t</addresses> \n";
				r += "\t\t\t\t\t</agent-identifier>\n";
			}
			r += "\t\t\t\t</intended-receiver> \n";
		}
		// if (this.received != null)
		// {
		// r += "\t\t\t\t<received>\n";
		// // if(this.received)
		// r += "\t\t\t\t</received>\n";
		// }

		return r;
	}
	public String toString()
	{
		return toXML();
	}

}
